package cn.kooki.app.duobao.util.manager;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.Bean.Result.ResultItem;
import cn.kooki.app.duobao.data.Bean.User.record.RecordItem;
import cn.kooki.app.duobao.data.Bean.cart.CartInfo;
import cn.kooki.app.duobao.util.StringUtils;

/**
 * Created by xiong on 2015/12/12.
 */
public class ShopCartManager {


    public CartChangeObservable changeObservable = new CartChangeObservable();

    private LinkedHashMap<String, CartInfo> carts = new LinkedHashMap<>();
    private LinkedList<String> ids = new LinkedList<>();
    private LinkedList<String> checkedids = new LinkedList<>();
    private static ShopCartManager manager;

    private ShopCartManager() {
    }

    public LinkedHashMap<String, CartInfo> getCarts() {
        return carts;
    }

    public int count;

    public static ShopCartManager getInstance() {
        if (manager == null) {
            manager = new ShopCartManager();
        }
        return manager;
    }

    public synchronized boolean addShopCart(GoodsItem goodsItem) {
        if (!carts.containsKey(goodsItem.getId())) {
            CartInfo info = new CartInfo();
            info.canyurenshu = goodsItem.getCanyurenshu();
            int count = StringUtils.nullSafeParseInt(goodsItem.getYunjiage());
            if (goodsItem.count > count) {
                info.count = goodsItem.count;
            } else {
                info.count = count;
            }
            info.id = goodsItem.getId();
            info.money = goodsItem.getMoney();
            info.qishu = goodsItem.getQishu();
            info.thumb = goodsItem.getThumb();
            info.title = goodsItem.getTitle();
            info.title2 = goodsItem.getTitle2();
            info.yunjiage = goodsItem.getYunjiage();
            info.zongrenshu = goodsItem.getZongrenshu();
            carts.put(goodsItem.getId(), info);
            ids.add(goodsItem.getId());
            changeObservable.notifyChanged(getToalCount());
            changeObservable.notifyCartChange();
            return true;
        } else {
            CartInfo item = carts.get(goodsItem.getId());
            int count = StringUtils.nullSafeParseInt(goodsItem.getYunjiage());
            if (goodsItem.count < count) {
                goodsItem.count = count;
            }
            int currt = StringUtils.nullSafeParseInt(item.canyurenshu);
            int total = StringUtils.nullSafeParseInt(item.zongrenshu);
            if (goodsItem.count + item.count <= total - currt) {
                item.count = item.count + goodsItem.count;
            } else {
                item.count = total - currt;
            }
            changeObservable.notifyChanged(getToalCount());
            changeObservable.notifyCartChange();
            return false;
        }

    }

    public synchronized boolean addShopCart(RecordItem goodsItem) {
        int count = StringUtils.nullSafeParseInt(goodsItem.yunjiage);
        if (!carts.containsKey(goodsItem.id)) {
            CartInfo info = new CartInfo();
            info.canyurenshu = goodsItem.canyurenshu;
            info.count = count;
            info.id = goodsItem.id;
            info.qishu = goodsItem.qishu;
            info.thumb = goodsItem.thumb;
            info.title = goodsItem.title;
            info.yunjiage = goodsItem.yunjiage;
            info.zongrenshu = goodsItem.zongrenshu;
            carts.put(goodsItem.id, info);
            ids.add(goodsItem.id);
            changeObservable.notifyChanged(getToalCount());
            changeObservable.notifyCartChange();
            return true;
        } else {
            CartInfo item = carts.get(goodsItem.id);
            int currt = StringUtils.nullSafeParseInt(item.canyurenshu);
            int total = StringUtils.nullSafeParseInt(item.zongrenshu);
            if (count + item.count <= total - currt) {
                item.count = item.count + count;
                changeObservable.notifyChanged(getToalCount());
                changeObservable.notifyCartChange();
            }
            return false;
        }

    }


    public synchronized void addShopCarts(ArrayList<GoodsItem> datas) {

        for (int i = 0; i < datas.size(); i++) {
            GoodsItem goodsItem = datas.get(i);
            if (!carts.containsKey(goodsItem.getId())) {
                CartInfo info = new CartInfo();
                info.canyurenshu = goodsItem.getCanyurenshu();
                int count = StringUtils.nullSafeParseInt(goodsItem.getYunjiage());
                if (goodsItem.count > count) {
                    info.count = goodsItem.count;
                } else {
                    info.count = count;
                }
                info.id = goodsItem.getId();
                info.money = goodsItem.getMoney();
                info.qishu = goodsItem.getQishu();
                info.thumb = goodsItem.getThumb();
                info.title = goodsItem.getTitle();
                info.title2 = goodsItem.getTitle2();
                info.yunjiage = goodsItem.getYunjiage();
                info.zongrenshu = goodsItem.getZongrenshu();
                carts.put(goodsItem.getId(), info);
                ids.add(goodsItem.getId());
                changeObservable.notifyChanged(getToalCount());
                changeObservable.notifyCartChange();

            } else {
                CartInfo item = carts.get(goodsItem.getId());
                int currt = StringUtils.nullSafeParseInt(item.canyurenshu);
                int total = StringUtils.nullSafeParseInt(item.zongrenshu);
                if (goodsItem.count + item.count <= total - currt) {
                    item.count = item.count + goodsItem.count;
                    changeObservable.notifyChanged(getToalCount());
                    changeObservable.notifyCartChange();
                }
            }
        }

    }


    public void removeSelectedCarts() {
        for (int i = 0; i < checkedids.size(); i++) {
            String id = checkedids.get(i);
            if (carts.containsKey(id)) {
                carts.remove(id);
                ids.remove(id);
            }
        }
        checkedids.clear();
        changeObservable.notifyChanged(getToalCount());
        changeObservable.notifyCartChange();
        changeObservable.notifyCheckChanged(checkedids.size(), true);
    }

    public synchronized boolean removeShopCart(GoodsItem goodsItem) {
        if (!carts.containsKey(goodsItem.getId())) {
            return false;
        } else {
            carts.remove(goodsItem.getId());
            ids.remove(goodsItem.getId());
            changeObservable.notifyChanged(getToalCount());
            return true;
        }
    }

    public void selectedCart(int postion) {
        String id = ids.get(postion);
        if (!checkedids.contains(id)) {
            checkedids.add(id);
        } else {
            checkedids.remove(id);
        }
        changeObservable.notifyCheckChanged(checkedids.size(), true);
    }

    public void selectedAllCarts(boolean isclear) {
        checkedids.clear();
        if (isclear) {
            checkedids.addAll(ids);
        }
        changeObservable.notifyCheckChanged(checkedids.size(), false);
    }

    public int getSelectCounts() {
        return checkedids.size();
    }

    public void changeCount(String id, int count) {
        if (carts.containsKey(id)) {
            CartInfo item = carts.get(id);
            item.count = count;
            changeObservable.notifyChanged(getToalCount());
        }
    }

    public void addCount(String id, int count) {
        if (carts.containsKey(id)) {
            CartInfo item = carts.get(id);
            item.count += count;
            changeObservable.notifyChanged(getToalCount());
        }
    }

    public synchronized int getToalCount() {
        count = 0;
        for (Map.Entry<String, CartInfo> entry : carts.entrySet()) {
            CartInfo value = entry.getValue();
            count += value.count;
        }
        return count;
    }


    public boolean checkSelected(String id) {

        return checkedids.contains(id);
    }

    public int getCartsSize() {
        return carts.size();
    }

    public CartInfo getItemAtPostion(int postion) {
        String id = ids.get(postion);
        CartInfo item = carts.get(id);
        return item;
    }

    public synchronized boolean clearCarts() {
        carts.clear();
        ids.clear();
        checkedids.clear();
        changeObservable.notifyChanged(carts.size());
        changeObservable.notifyCartChange();
        changeObservable.notifyCheckChanged(checkedids.size(), true);
        return true;
    }
}
